﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PlayDeezGames.Common;
using System.Xml.Linq;

namespace PlayDeezGames.Common
{
    public class Property : EntityBase
    {
        public class Factory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, XElement theXElement)
            {
                return new Property(theParent as Descriptor, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new Property(theParent as Descriptor);
            }
            private Factory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.CommonFactories.PropertyFactory, new Property.Factory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.CommonFactories.PropertyFactory);
            }
        }

        public Descriptor Descriptor
        {
            get
            {
                return ParentEntity as Descriptor;
            }
        }

        private object payload = null;
        public object Payload
        {
            get
            {
                return payload;
            }
            set
            {
                payload = value;
            }
        }

        private string type = string.Empty;
        public string Type
        {
            get
            {
                return type;
            }
            set
            {
                type = value;
            }
        }

        public string Identifier
        {
            get
            {
                foreach (var property in Descriptor.Properties)
                {
                    if (Descriptor[property] == this)
                    {
                        return property;
                    }
                }
                throw new IndexOutOfRangeException();
            }
        }

        protected Property(Descriptor parent)
            : base(parent)
        {
        }
        protected Property(Descriptor parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }

        protected override XElement OnToXElement()
        {
            XElement result = new XElement(Names.CommonXNames.Property);
            result.Add(new XAttribute(PlayDeezGames.Common.Names.CommonXNames.Factory, Names.CommonFactories.PropertyFactory));
            result.Add(new XAttribute(Names.CommonXNames.Identifier, Identifier));
            result.Add(new XAttribute(Names.CommonXNames.Type, Type));
            PropertyFactories.Save(Type, result, Payload);
            return result;
        }

        protected override void OnFromXElement(XElement theXElement)
        {
            type = theXElement.Attribute(Names.CommonXNames.Type).Value;
            payload = PropertyFactories.Load(Type, theXElement);
            Descriptor[theXElement.Attribute(Names.CommonXNames.Identifier).Value] = this;
        }

        public static Property Create(Descriptor theParent)
        {
            return EntityFactories.Create(theParent, Names.CommonFactories.PropertyFactory) as Property;
        }

        public static Property Load(Descriptor theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as Property;
        }
    }
}
